<?php
//注释呢
namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;  

class TopicController extends Controller
{

    //查出话题的方法
    public function usertoc(Request $request){
        //开启session
        session_start();
        $id = $_SESSION['id'];

        //定义一个空数组
        $key1 = [];
        $arr = [];
        $array=[];


        //通过用户id查出来他关注的话题
        $data=DB::table('userTopic')
              ->where('uid',$id)
              ->get();

        //遍历数据
        foreach ($data as $k => $v) {
               $tid=$v->tid;//取出每一个tid
              // var_dump($tid);
                //通过tid查询话题表的信息
               $data1=DB::table('topic')
                    ->where('id',$tid)
                    ->get();
               // var_dump($data1);
               // //遍历查出来的话题信息
               foreach ($data1 as $k => $v) {     
               $title=$v->title; 
               $arr[]=$title;
                  }
               $key1[] = $tid;

        }

       //合并数组
        $data1=array_combine($key1,$arr);
       //解析模板带数据过去
        // var_dump($data1);
        //获取tid
         $tid=$request->input('tid');
         // var_dump($tid);
         //var_dump($tid);
        if ($tid) {
          //开始查询
         $data=DB::table('issue')->where('tid',$tid)->get();
      

         //根据问题查出来答案
         // $array=[];
         foreach ($data as $k => $v) {
           $id=$v->id;
           //话题标题压入数组中
           $array[$k]['title']=$v->atitle;
            $array[$k]['id']=$v->id;
           //开始查询答案
           $res = DB::select("select *,max('pass') as sum from answer where Iid = ".$id);//根据问题查出答案

           foreach ($res as $key => $value) {
              //开始插入到数组中 相关数据  答案相关数据
              $array[$k]['aid']=$value->id;
              $array[$k]['pass']=$value->pass;
              $array[$k]['content']=$value->content;
              $uid=$value->uid;
              //查询用户详情表
              $res1=DB::table('userDetails')->where('uid',$uid)->get();
                foreach ($res1 as $key => $value) {
                  //把用户数据压入数组中
                  $array[$k]['uname']=$value->nickName;
                  $array[$k]['jj']=$value->introduce;
                }


           }
           
         }
         // var_dump($array);
         }else{
            //交换数组的键和值
            $data2  =  array_flip ( $data1 );
            $num=reset($data2);
             //开始查询
            // $data=DB::table('issue')->where('tid',$num)->get();
             //开始查询
            // var_dump($num);
         $data=DB::table('issue')->where('tid',$num)->get();
          
         //根据问题查出来答案
         // var_dump($data);
         $array=[];
         foreach ($data as $k => $v) {
           $id=$v->id;
           //话题标题压入数组中
           $array[$k]['title']=$v->atitle;
           $array[$k]['id']=$v->id;
           // //开始查询答案
           // $res=DB::table('answer')->select('*')->where('Iid',$id)->max('pass');
            // $res2=DB::table('answer')->where('Iid',$id)->get();
           //  foreach($res2 as $k=>$v){
           //    $array[$k]['content2']=$v->content;
           //  }
           $res = DB::select("select *,max('pass') as sum from answer where Iid = ".$id);

           foreach ($res as $key => $value) {
              // var_dump($value);
              //开始插入到数组中 相关数据  答案相关数据
              $array[$k]['aid']=$value->id;
              $array[$k]['pass']=$value->pass;
              $array[$k]['content']=$value->content;
              $aid=$value->id;

              $uid=$value->uid;
              //查询用户详情表
              $res1=DB::table('userDetails')->where('uid',$uid)->get();

                foreach ($res1 as $key => $value) {
                  //把用户数据压入数组中
                  $array[$k]['uname']=$value->nickName;
                  $array[$k]['jj']=$value->introduce;
                }
              
                }
                $res3=DB::table('comment')->where('aid',$aid)->get();
                foreach ($res3 as $key => $value) {
                  //把用户数据压入数组中
                  $array[$k]['contentp']=$value->contentp;
                  // $array[$k]['jj']=$value->introduce;
                  // var_dump($res3);
           }

         
           
         }

         }
         // dd($array);
         // var_dump($res3);
         //传的什么值 写清楚了 data1查的话题信息 array话题下的详细信息 
        return view('/home.topic.topic',['data1'=>$data1,'array'=>$array]);
    }

    public function getUsert()
    { 
      
      //接受数据
       $data = DB::table('topic')->get();
      return view('home/topic/usert',['data'=>$data]);
    }
    //用ajax实现加关注的方法
    public function getIntopic(Request $request){
      //接受传过来的tid
      $tid=$request->input('tid');
      
      //获取用户的uid
      session_start();
      $uid=$_SESSION['id'];


      //然后进行数据添加操作
      $res=DB::table('userTopic')->insertGetId(['uid'=>$uid,'tid'=>$tid]);
      if ($res) {
        echo $res;
      }
    }

     //用ajax实现取消关注的方法
    public function getDetopic(Request $request)
    {
      //接受传过来的tid
      $tid=$request->input('tid');
      // 获取用户的uid
      session_start();
      $uid=$_SESSION['id'];
      //然后进行数据删除操作
      $res=DB::table('userTopic')->where(['tid'=>$tid])->delete();
     if ($res) {
        echo $res;
      }
      
    }

       public function getList(Request $request)
    { 
        
        //定义空数组存放数据
       $array = [];
       $id = $request->input('id');
       $data = DB::table('topic')->where('id',$id)->get();

       //获取id下的问题
       // $tid =  $request->input('id');
       // var_dump($tid);
       $data1 = DB::table('issue')->where('tid',$id)->get();
       foreach($data1 as $k=>$v){
        $id = $v->id;

        // var_dump($id);
       $data3 = DB::table('answer')->where('Iid',$id)->get();
         foreach ($data3 as $ka => $va) {
          $array[$ka]['content'] = $va->content;
          $array[$ka]['pass'] = $va->pass;
          $uid=$v->uid;
          // $id = $v->id;
          // var_dump($id);
        //查询用户详情表
        $data2=DB::table('userdetails')->where('uid',$uid)->get();

         foreach ($data2 as $ku => $vu){
          $array[$ku]['uname'] = $vu->nickName;
          $array[$ku]['jj'] = $vu->introduce;

         // }
      
    }

    }
          return view('/home/topic/list',['data'=>$data,'data1'=>$data1,'array'=>$array]);

  }

}
}